package function;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import UserDatabase.User;
import UserDatabase.UserDatabase;
import login.LoginFrame;

public class MRegisterFrame extends JFrame{
	//姓名，身份，性别，年龄，单元号，房间号，密码，确认密码
		private JLabel l_name,l_type,l_sex,l_age,l_uno,l_hno,l_password1,l_password2;
		private JTextField t_name,t_age,t_uno,t_hno;
		JRadioButton r_male,r_female;//单选按钮：男士，女士
		private JComboBox <String> c_type;
		private JPasswordField p_password1,p_password2;
		private JButton b_reg,b_reset;//按钮：注册,重置
		
		public MRegisterFrame() {
			this.setSize(400,500);
			this.setTitle("注册界面");
			this.setLocation(500,200);
			init();
			this.setVisible(true);
		}
		
		public void init() {
			l_name = new JLabel("姓名",JLabel.CENTER);
			l_type = new JLabel("身份",JLabel.CENTER);
			l_sex = new JLabel("性别",JLabel.CENTER);
			l_age = new JLabel("年龄",JLabel.CENTER);
			l_uno = new JLabel("单元号",JLabel.CENTER);
			l_hno = new JLabel("房间号",JLabel.CENTER);
			
			l_password1 = new JLabel("密码",JLabel.CENTER);
			l_password2 = new JLabel("确认密码",JLabel.CENTER);
			
			t_name = new JTextField();
			t_age = new JTextField();
			t_uno = new JTextField();
			t_hno = new JTextField();
			
			//归组才能单选
			ButtonGroup bg = new ButtonGroup();
			r_male = new JRadioButton("男士");
			r_female = new JRadioButton("女士");
			bg.add(r_male);
			bg.add(r_female);
			
			//中间容器存储两个选项（二合一）
			JPanel p_sex = new JPanel();
			p_sex.add(r_male);
			p_sex.add(r_female);
			
			c_type = new JComboBox<String>();
			c_type.addItem("物业");
			
			p_password1 = new JPasswordField();
			p_password2 = new JPasswordField();
			
			b_reg = new JButton("注册");
			b_reg.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent e) {
					//从窗体收集用户信息
					String name = t_name.getText();
					Object type = c_type.getSelectedItem();
					String password1 = new String(p_password1.getPassword());
					String password2 = new String(p_password2.getPassword());
					String age = t_age.getText();
					String uno = t_uno.getText();
					String hno = t_hno.getText();
					
					JFrame f = new JFrame();
					if(password1.equals("")||password2.equals("")) {
					
						JOptionPane.showMessageDialog(f,"密码不能为空！","提示框",JOptionPane.WARNING_MESSAGE);
					
					}else if(!password1.equals(password2)) {
						
						JOptionPane.showMessageDialog(f,"两次密码应一致！","提示框",JOptionPane.WARNING_MESSAGE);
							
						p_password1.setText("");
						
						p_password2.setText("");
						
						return ;
					}
					
					String sex;
					if(r_male.isSelected()) {
						sex = "男";
					}else {
						sex = "女";
					}
					
					//用数据库导入数据
					if(!name.equals("")&&!password1.equals("")&&!password2.equals("")&&!age.equals("")&&!uno.equals("")&&!hno.equals("")) {
						User u = new User(name,type,sex,age,uno,hno,password1);
						UserDatabase udb = new UserDatabase();
						udb.add(u);
						JOptionPane.showMessageDialog(f,"注册成功！","提示框",JOptionPane.WARNING_MESSAGE);
						setVisible(false);
					}
				}
			});
			b_reset = new JButton("重置");
			b_reset.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent e) {
					t_name.setText(null);
					t_age.setText(null);
					t_uno.setText(null);
					t_hno.setText(null);
					bg.clearSelection();
					p_password1.setText(null);
					p_password2.setText(null);
				}
				
			});
			
			JPanel p = new JPanel();
			p.setLayout(new GridLayout(9,2,5,5));
			
			p.add(l_name);
			p.add(t_name);
			
			p.add(l_type);
			p.add(c_type);
			
			p.add(l_sex);
			p.add(p_sex);
			
			p.add(l_age);
			p.add(t_age);
			
			
			p.add(l_uno);
			p.add(t_uno);
			
			p.add(l_hno);
			p.add(t_hno);
			
			p.add(l_password1);
			p.add(p_password1);
			
			p.add(l_password2);
			p.add(p_password2);
			
			p.add(b_reg);
			p.add(b_reset);
			
			this.setLayout(null);
			p.setBounds(19, 10, 350, 450);
			this.add(p);
			this.setResizable(false);
			
		}
}
